<template>
    <ModalFormTemplate
      :open="open"
      :formConfig="formConfig"
      :title="title"
      :width="width"
      @cancel="onCancel"
      @finish="onFinish"
      ref="modalRef"
    />
</template>
<script setup lang="ts">
  import { computed, ref, watch } from 'vue';
  import ModalFormTemplate from '@/components/template/form/modal-form-template.vue';
  import { bizApi } from '@/api/biz';

  const title ="代理商发票修改";//弹窗标题
  const width = 600;//弹窗宽度
  const modalRef = ref();//通用模态弹窗组件引用，用与调用模态弹窗组件内部方法
  const open = ref(false);//控制弹窗显示、隐藏
  const emits = defineEmits<{ dataReload: []; }>();//通知调用组件(父组件) 执行 父组件 的 @dataReload 的方法
  const editId = ref(0); //要修改的记录ID
  
  defineExpose({ show });//暴露接口 show(显示弹窗) 方法，供 父组件 调用

  /* [关闭弹窗] 1. 重置表单 2. 关闭弹窗 */
  function onCancel() {
    modalRef.value.resetForm();
    open.value = false;
  }

  /* [显示弹窗] 1. 显示弹窗  */
  function show(id: number) {
    editId.value = id;
    open.value = true;
  }

  /* [监听弹窗显示] open = true 时，调用 getDataInfo 获取数据 */
  watch(() => open.value, (val) => {
    if (val === true) {
      getDataInfo();
    }
  })
  
  /* [获取修改数据] 调用子组件 getDataInfo 方法详情 */
  function getDataInfo() {
    modalRef.value?.getDataInfo(getAction, getParams.value);
  }
  const getAction = "base.agent.agentinvoice.ServiceImpl.get";//get接口 方法名
  const getParams = computed(()=>({ agent_invoice_id: editId.value }));//get接口 参数

  /* [提交弹窗] 1. 调用子组件 提交方法 2. 提交成功后，关闭弹窗 3. 刷新父组件数据 */
  async function onFinish() {
    const params = modalRef.value?.getFormParams();
    const mergedParams = {...getParams.value,...params};//合并提交参数
    try {
      const ret = await modalRef.value?.postFormData(updateAction, mergedParams);
      if (ret === true) {
        onCancel();
        emits('dataReload');
      }
    } catch (error) {}
  }
  const updateAction = 'base.agent.agentinvoice.ServiceImpl.update';//update接口 方法名

  /* [表单配置] */
  const formConfig = computed(() => [
    {
      title: '代理商',
      dataIndex: 'agent_id',
      type: 'select' as const,
      required: true,
      props: {
        options: {
          action: "base.agent.agent.ServiceImpl.listSelectAgent",
          params: {}
        }
      },
    },
    {
      title: '发票形式',
      dataIndex: 'invoice_form',
      type: 'select' as const,
      required: true,
      props: {
        options: [
          {label:"电子发票",value:1},
          {label:"纸质发票",value:2}
        ]
      }
    },
    {
      title: '发票类型',
      dataIndex: 'invoice_type',
      type: 'select' as const,
      required: true,
      props: {
        options: [
          {label:"专票",value:1},
          {label:"普票",value:2}
        ]
      }
    },
    {
      title: '纳税人标识',
      dataIndex: 'taxpayer_id',
      type: 'input' as const,
      maxlength: 50,
    },
    {
      title: '联系人姓名',
      dataIndex: 'contact_name',
      type: 'input' as const,
      maxlength: 100,
    },
    {
      title: '联系人电话',
      dataIndex: 'contact_phone',
      type: 'input-number' as const,
      maxlength: 13
    },
    {
      title: '发票邮寄地址',
      dataIndex: 'post_address',
      type: 'input' as const,
      maxlength: 250,
    },
    {
      title: '邮编',
      dataIndex: 'post_code',
      type: 'input-number' as const,
      maxlength: 6,
    },
    {
      title: '开票联系电话',
      dataIndex: 'invoice_phone',
      type: 'input-number' as const,
      maxlength: 13,
    },
    {
      title: '开票地址',
      dataIndex: 'invoice_address',
      type: 'input' as const,
      maxlength: 250,
    },
    {
      title: '开户银行名称',
      dataIndex: 'account_bank',
      type: 'input' as const,
      maxlength: 100,
    },
    {
      title: '账号信息',
      dataIndex: 'account_no',
      type: 'input' as const,
      maxlength: 50,
    },
    {
      title: '邮箱',
      dataIndex: 'email',
      type: 'input' as const,
      maxlength: 111,
      required: true,
    },
    {
      title: '备注',
      dataIndex: 'remark',
      type: 'input' as const,
      maxlength: 250,
    },
  ]);
</script>
<style lang="less" scoped></style>
